// JavaScript Document

/**
 * import compontent 
 */
Vsg.importClass(
	[
		"Vsg.DataSourceEntity.DataSourceEntityBuilder",
		"Vsg.EntityRecord.StockTopAssembly",
		"Vsg.Component.StockTopAssemblyPanel"
	]
);

function getModelPanel(url) {
	try {
		var $dsEntity = new Vsg.DataSourceEntity.DataSourceEntityBuilder();
		$dsEntity.setUrl( url);
		$dsEntity.setEntityRecord( new Vsg.EntityRecord.StockTopAssembly() );	
		var $ds = $dsEntity.newAjaxDataSource();
		
		// --- defined the grid panel ---
		var gp = new Vsg.Component.StockTopAssemblyPanel({
        	store: $ds,
			frame:false,
			border:false,			
			//width:200,
        	loadMask: true,
        	viewConfig: {
            	forceFit:true
        	}						
		});
		
		gp.on("cellclick" , Vsg.getDefinedEvent("Vsg.Component.StockTopAssemblyPanel.evt_rowclick"));
				
		return gp;	
	} catch (e) {
		alert("Get model panel error: "  );
	}	 
}


/**
 * this method using by parent window ,defined the global
 */
function getSelectedCategoryRecord() {
	var grids = viewport.findByType(Vsg.Component.StockTopAssemblyPanel,true);
	var grid = null;
	var record = null;
	for (var i = 0 ; i < grids.length ; i++) {
		grid = grids[i];
		var selModel = grid.getSelectionModel();
		record = selModel.getSelected();
		if (record != null) {
			break;
		}	
	}

	return record;
}


var viewport = null;

/** ===============================
 * init ui interface 
 * ================================
 */
function init(){

	try {
	

    	 viewport = new Ext.Viewport( {
			layout:"border",
			border:false,
			items:[{
				layout:'column',
				region:"center",
				border:false,
				autoScroll:true,
				margins:"5 5 5 5",
				items:[{
					columnWidth:0.5,
                	style:'padding:5px 5px 5px 5px',
					layout:"border",
					border:false,
					height:500,
					items:[{
						region:"center",
						margins:"0 0 5 0",
						title:"近3天出现频率",
						layout:"fit",
						items:[
							getModelPanel(UrlMKey['stk-assembly-topaction']+"?top=3&type=trades")
						]
						},{
						region:"south",
						layout:"fit",
						margins:"0 0 0 0",
						height:250,
						title:"近20天出现频率",
						items:[
							getModelPanel(UrlMKey['stk-assembly-topaction']+"?top=20&type=trades")
						]
					}]
				},{
					columnWidth:0.5,
					layout:"border",
					border:false,
					height:500,
                	style:'padding:5px 5px 5px 5px',
					items:[{
						region:"center",
						margins:"0 0 5 0",
						layout:"fit",
						title:"近5天新出现频率",
						items:[
							getModelPanel(UrlMKey['stk-assembly-topaction']+"?notTop=20&top=5&type=trades")
						]
					},{
						region:"south",
						layout:"fit",
						margins:"0 0 0 0",
						height:250,
						title:"近10天新出现频率",
						items:[
							getModelPanel(UrlMKey['stk-assembly-topaction']+"?notTop=20&top=10&type=trades")
						]
					}]
				}]
			}]
	});
	
	} catch (e) {
		alert('出现系统性错误，请联系管理人员!');
		//Ext.log("Initial UI error: " + e);
	}
}
// --- run the init method
Vsg.run( init );


/**
 *  defined row click 
 * @param {Object} $this
 * @param {Object} rowIndex
 * @param {Object} event
 */
Vsg.definedEvent("Vsg.Component.StockTopAssemblyPanel.evt_rowclick" , function($this, rowIndex, event) {
	var parentContainer = $this.findParentByType(Ext.Viewport);

	var grids = parentContainer.findByType(Vsg.Component.StockTopAssemblyPanel,true);
	var grid = null;
	for (var i = 0 ; i < grids.length ; i++) {
		grid = grids[i];
		if ($this != grid) {
			// --- set unselect model ---
			var selModel = grid.getSelectionModel();
			selModel.clearSelections();
		}
		
	}	
});
